package com.example.beat_x3.Fragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beat_x3.Adapter.FeedbackAdapter;
import com.example.beat_x3.Client.GetResult;
import com.example.beat_x3.R;

import Entity.Inf;
import Entity.Replys;

public class FeedbackFragment extends MyFragment {
    FeedbackAdapter feedbackAdapter;
    public Handler myHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                feedbackAdapter.refreshOne();
            }
        }
    };

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_feedback, null);
        RecyclerView recyclerView = view.findViewById(R.id.feedback_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(Inf.context));
        feedbackAdapter = new FeedbackAdapter();
        recyclerView.setAdapter(feedbackAdapter);
        view.findViewById(R.id.feedback_backbutton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(Inf.centerFragment);
            }
        });
        new QueryReplysThread().start();
        return view;
    }

    class QueryReplysThread extends Thread {
        @Override
        public void run() {
            Replys res = GetResult.queryReplyGR(Inf.stuInf.StudentAccount);
            if (res != null) {
                feedbackAdapter.res = res;
                for(String x:res.complaints){
                    System.out.println(x);
                }
                myHandler.sendEmptyMessage(0);
            }
        }
    }
}
